﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
using System.Text;
using System.Web.UI.DataVisualization.Charting;
namespace Web
{
    public partial class AreaChartBase : System.Web.UI.Page
    {
        protected int examroomid = 0, areaid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "StatistScore"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (!PageValidate.QueryStringIsEmpty("areaid"))
            {
                try
                {
                    areaid = int.Parse(PageValidate.QueryString("areaid"));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            if (!PageValidate.QueryStringIsEmpty("examroomid"))
            {
                try
                {
                    examroomid = int.Parse(PageValidate.QueryString("examroomid"));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            if (!IsPostBack)
            {
                if (examroomid > 0 && areaid > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    StringBuilder gsb = new StringBuilder();
                    IList<SchoolInfo> schoolInfos = School.GetSchoolInfoByAreaId(areaid);
                    IList<StudentScoreExtendInfo> studentscoreinfos = new List<StudentScoreExtendInfo>();
                    studentscoreinfos = StudentScoreExtend.GetList(examroomid, areaid, 0, 0, 0, 0, 0, 0);
                    IList<int> goodNum = GoodAreaScoreSort(studentscoreinfos);
                    IList<int> midNum = MidAreaScoreSort(studentscoreinfos);
                    IList<int> lowNum = LowAreaScoreSort(studentscoreinfos);
                    IList<int> endNum = EndAreaScoreSort(studentscoreinfos);
                    int num = 1;
                    foreach (SchoolInfo schoolInfo in schoolInfos)
                    {
                        IList<StudentScoreExtendInfo> schoolscoreInfos = studentscoreinfos.Where(t => t.SchoolId == schoolInfo.Id).ToList();
                        if (schoolscoreInfos.Count == 0)
                        {
                            continue;
                        }
                        string[] data = GetSchoolScore(schoolscoreInfos);
                        sb.Append("<tr>");
                        sb.Append("<td>");
                        sb.Append(num);
                        sb.Append("</td>");

                        sb.Append("<td class='text_left'>");
                        sb.Append(schoolInfo.Name);
                        sb.Append("</td>");

                        sb.Append("<td>");
                        sb.Append(schoolscoreInfos.Count);
                        sb.Append("</td>");

                        sb.Append("<td>");
                        sb.Append(data[10]);
                        sb.Append("</td>");

                        sb.Append("<td>");
                        sb.Append(data[4] + "(" + data[9] + "%)");
                        sb.Append("</td>");

                        sb.Append("<td>");
                        sb.Append(data[3] + "(" + data[8] + "%)");
                        sb.Append("</td>");

                        sb.Append("<td>");
                        sb.Append(data[2] + "(" + data[7] + "%)");
                        sb.Append("</td>");

                        sb.Append("<td>");
                        sb.Append(data[1] + "(" + data[6] + "%)");
                        sb.Append("</td>");

                        sb.Append("<td>");
                        sb.Append(data[0] + "(" + data[5] + "%)");
                        sb.Append("</td>");
                        sb.Append("</tr>");
                        //表格二
                        string[] levelData = GetLevelSchool(schoolscoreInfos, goodNum, midNum, lowNum, endNum);
                        gsb.Append("<tr>");
                        gsb.Append("<td>");
                        gsb.Append(num);
                        gsb.Append("</td>");

                        gsb.Append("<td>");
                        gsb.Append(schoolInfo.Name);
                        gsb.Append("</td>");

                        gsb.Append("<td>");
                        gsb.Append(schoolscoreInfos.Count);
                        gsb.Append("</td>");

                        gsb.Append("<td>");
                        gsb.Append(data[10]);
                        gsb.Append("</td>");

                        gsb.Append("<td>");
                        gsb.Append(levelData[0]);
                        gsb.Append("</td>");

                        gsb.Append("<td>");
                        gsb.Append(levelData[1]);
                        gsb.Append("</td>");

                        gsb.Append("<td>");
                        gsb.Append(levelData[2]);
                        gsb.Append("</td>");

                        gsb.Append("<td>");
                        gsb.Append(levelData[3] + "%");
                        gsb.Append("</td>");

                        gsb.Append("<td>");
                        gsb.Append(levelData[4]);
                        gsb.Append("</td>");

                        gsb.Append("<td>");
                        gsb.Append(levelData[5] + "%");
                        gsb.Append("</td>");

                        gsb.Append("<td>");
                        gsb.Append(levelData[6]);
                        gsb.Append("</td>");

                        gsb.Append("<td>");
                        gsb.Append(levelData[7] + "%");
                        gsb.Append("</td>");

                        gsb.Append("<td>");
                        gsb.Append(levelData[8]);
                        gsb.Append("</td>");

                        gsb.Append("<td>");
                        gsb.Append(levelData[9] + "%");
                        gsb.Append("</td>");

                        gsb.Append("</tr>");
                        Series baseSeries = new Series()
                        {
                            Name = schoolInfo.Name,
                            ChartType = SeriesChartType.Spline,
                            XValueType = ChartValueType.Double,
                            BorderWidth = 2,
                            ChartArea = "ChartArea1"
                        };
                        baseSeries.Points.AddY(data[5]);
                        baseSeries.Points.AddY(data[6]);
                        baseSeries.Points.AddY(data[7]);
                        baseSeries.Points.AddY(data[8]);
                        baseSeries.Points.AddY(data[9]);
                        Chart1.Series.Add(baseSeries);

                        Series levelSeries = new Series()
                        {
                            Name = schoolInfo.Name,
                            ChartType = SeriesChartType.StackedColumn,
                            Label = "校-" + num.ToString(),
                            LabelToolTip = schoolInfo.Name,
                        };
                        levelSeries["PointWidth"] = "0.4";
                        levelSeries.Points.AddY(double.Parse(levelData[3]));
                        levelSeries.Points.AddY(double.Parse(levelData[5]));
                        levelSeries.Points.AddY(double.Parse(levelData[7]));
                        levelSeries.Points.AddY(double.Parse(levelData[9]));
                        Chart2.Series.Add(levelSeries);
                        num++;
                    }
                    DataList.InnerHtml = sb.ToString();
                    LevelDataList.InnerHtml = gsb.ToString();
                }
                else
                {
                    Response.Redirect("AdminExamRoom.aspx");
                }
            }
        }
        private IList<int> GoodAreaScoreSort(IList<StudentScoreExtendInfo> scoreInfos)
        {
            IList<int> result = new List<int>();
            scoreInfos = scoreInfos.OrderByDescending(t => t.CountScore).ToList();
            int num =Convert.ToInt32(scoreInfos.Count*0.05);
            for (int i = 0; i < num; i++)
            {
                result.Add(scoreInfos[i].StudentId);
            }
            return result;
        }

        private IList<int> MidAreaScoreSort(IList<StudentScoreExtendInfo> scoreInfos)
        {
            IList<int> result = new List<int>();
            scoreInfos = scoreInfos.OrderByDescending(t => t.CountScore).ToList();
            int star = Convert.ToInt32(scoreInfos.Count * 0.05);
            int end = Convert.ToInt32(scoreInfos.Count * 0.2);
            for (int i = star; i < end; i++)
            {
                result.Add(scoreInfos[i].StudentId);
            }
            return result;
        }

        private IList<int> LowAreaScoreSort(IList<StudentScoreExtendInfo> scoreInfos)
        {
            IList<int> result = new List<int>();
            scoreInfos = scoreInfos.OrderBy(t => t.CountScore).ToList();
            int star = Convert.ToInt32(scoreInfos.Count * 0.05);
            int end = Convert.ToInt32(scoreInfos.Count * 0.2);
            for (int i = star; i < end; i++)
            {
                result.Add(scoreInfos[i].StudentId);
            }
            return result;
        }

        private IList<int> EndAreaScoreSort(IList<StudentScoreExtendInfo> scoreInfos)
        {
            IList<int> result = new List<int>();
            scoreInfos = scoreInfos.OrderBy(t => t.CountScore).ToList();
            int num = Convert.ToInt32(scoreInfos.Count * 0.05);
            for (int i = 0; i < num; i++)
            {
                result.Add(scoreInfos[i].StudentId);
            }
            return result;
        }

        private string[] GetLevelSchool(IList<StudentScoreExtendInfo> scoreInfos, IList<int> good, IList<int> mid, IList<int> low, IList<int> end)
        {
            int g = scoreInfos.Where(t => GetArray<int>.Get(good).Contains(t.StudentId)).Count();
            decimal max = scoreInfos.Max(t => t.CountScore);
            decimal min = scoreInfos.Min(t => t.CountScore);
            decimal g_percent = 0;
            if(g > 0)
                g_percent = Math.Round(decimal.Parse(g.ToString()) / decimal.Parse(scoreInfos.Count.ToString()) * 100, 1);
            int m = scoreInfos.Where(t => GetArray<int>.Get(mid).Contains(t.StudentId)).Count();
            decimal m_percent = 0;
            if (m > 0)
                m_percent = Math.Round(decimal.Parse(m.ToString()) / decimal.Parse(scoreInfos.Count.ToString()) * 100, 1);
            int l = scoreInfos.Where(t => GetArray<int>.Get(low).Contains(t.StudentId)).Count();
            decimal l_percent = 0;
            if (l > 0)
                l_percent = Math.Round(decimal.Parse(l.ToString()) / decimal.Parse(scoreInfos.Count.ToString()) * 100, 1);
            int e = scoreInfos.Where(t => GetArray<int>.Get(end).Contains(t.StudentId)).Count();
            decimal e_percent = 0;
            if (e > 0)
                e_percent = Math.Round(decimal.Parse(e.ToString()) / decimal.Parse(scoreInfos.Count.ToString()) * 100, 1);
            return new string[] { max.ToString(), min.ToString(), g.ToString(), g_percent.ToString(), m.ToString(), m_percent.ToString(), l.ToString(), l_percent.ToString(), e.ToString(), e_percent.ToString() };
        }
        private string[] GetSchoolScore(IList<StudentScoreExtendInfo> scoreInfos)
        {
            int score0 = 0, score1 = 0, score2 = 0, score3 = 0, score4 = 0;
            decimal percent0 = 0, percent1 = 0, percent2 = 0, percent3 = 0, percent4 = 0;
            decimal count = 0, avery = 0;
            foreach (StudentScoreExtendInfo info in scoreInfos)
            {
                count += info.CountScore;
                if (info.CountScore < 60)
                {
                    score0++;
                }
                if (info.CountScore >= 60 && info.CountScore <= 70)
                {
                    score1++;
                }
                if (info.CountScore > 70 && info.CountScore <= 80)
                {
                    score2++;
                }
                if (info.CountScore > 80 && info.CountScore <= 90)
                {
                    score3++;
                }
                if (info.CountScore > 90)
                {
                    score4++;
                }
            }
            avery = Math.Round(count / decimal.Parse(scoreInfos.Count.ToString()));
            if (score0 > 0)
            {
                percent0 = Math.Round(decimal.Parse(score0.ToString()) / decimal.Parse(scoreInfos.Count.ToString()) * 100, 1);
            }
            if (score1 > 0)
            {
                percent1 = Math.Round(decimal.Parse(score1.ToString()) / decimal.Parse(scoreInfos.Count.ToString()) * 100, 1);
            }
            if (score2 > 0)
            {
                percent2 = Math.Round(decimal.Parse(score2.ToString()) / decimal.Parse(scoreInfos.Count.ToString()) * 100, 1);
            }
            if (score3 > 0)
            {
                percent3 = Math.Round(decimal.Parse(score3.ToString()) / decimal.Parse(scoreInfos.Count.ToString()) * 100, 1);
            }
            if (score4 > 0)
            {
                percent4 = Math.Round(decimal.Parse(score4.ToString()) / decimal.Parse(scoreInfos.Count.ToString()) * 100, 1);
            }
            return new string[] { score0.ToString(), score1.ToString(), score2.ToString(), score3.ToString(), score4.ToString(), percent0.ToString(), percent1.ToString(), percent2.ToString(), percent3.ToString(), percent4.ToString(), avery.ToString() };
        }
    }
}
